package com.vip.service.kafka;


import com.alibaba.fastjson.JSONObject;
import com.vip.dao.action.domain.SysOperLog;
import com.vip.service.action.SysOperLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class KafKaConsumer {

    @Autowired
    private SysOperLogService sysOperLogService;


    @KafkaListener(topics = {"sys-log"})
    public void handlerCommentMessage(ConsumerRecord record){
        if(record == null || record.value() == null){
            log.info("kafkaconsumer----->>>消息为空");
            return;
        }

        SysOperLog operLog = JSONObject.parseObject(record.value().toString(),SysOperLog.class);
        if(operLog == null){
            log.error("消息格式错误");
            return;
        }

        sysOperLogService.addOneOperLog(operLog);
        log.info("日志追加成功");
    }


}
